#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test truncate command (replaces old purge action=truncate ...)
#
# We run several jobs on different volumes, than during the last job
# we run a truncate command.
#
#
TestName="truncate2-test"
JobName=NightlySave
. scripts/functions

cwd=`pwd`
scripts/cleanup
scripts/copy-2disk-virtual-confs

echo $src > $tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "ActionOnPurge", "Truncate", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData", "No", "Job")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Volume Size", "500M", "Device")'
start_test

cat >tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=vDrive-1 volume=TestVolume001 drive=0 slot=0 pool=Default
@########################################################
@# Run a first job on TestVolume001
@########################################################
run level=full job=$JobName storage=vDrive-1 yes
wait
messages
@#########################################################
@# Run a second job on TestVolume002
@#########################################################
@$out $tmp/log3.out
label storage=vDrive-1 volume=TestVolume002 drive=0 slot=0 pool=Default
update volume=TestVolume001 volstatus=Used
messages
@$out $tmp/log1.out
run level=full job=$JobName storage=vDrive-1 yes
wait
messages
@#########################################################
@# Run a 3th job on TestVolume003
@#########################################################
@$out $tmp/log3.out
update volume=TestVolume002 volstatus=Used
label storage=vDrive-1 volume=TestVolume003 drive=0 slot=0 pool=Default
messages
@$out $tmp/log1.out
run level=full job=$JobName storage=vDrive-1 yes
wait
messages
@#########################################################
@# Run a 4th job
@#########################################################
@$out $tmp/log5.out
update volume=TestVolume003 volstatus=Used
label storage=vDrive-1 volume=TestVolume004 drive=0 slot=0 pool=Default
messages
@$out $tmp/log1.out
run level=full job=$JobName storage=vDrive-1 yes
wait
messages
list volume
@$out $tmp/log1.out
update volume=TestVolume004 volstatus=Used
label storage=vDrive-1 volume=TestVolume005 drive=0 slot=0 pool=Default
setbandwidth limit="10000 kb/s" client
run level=full job=$JobName storage=vDrive-1 yes
@sleep 5
@$out $tmp/log6.out
@#########################################################
@# tests
@#########################################################
update volume=TestVolume002 actiononpurge=none
purge volume=TestVolume001
purge volume=TestVolume002
purge volume=TestVolume003
purge volume=TestVolume004
list volume
sql
select VolumeName, VolStatus, ActionOnPurge FROM Media;

setdebug level=100 director
quit
END_OF_DATA

run_bacula

if test "$debug" -eq 1 ; then
  echo "Volume sizes before truncate ..."
  cd ${tmp}
  ls -l TestVolume*
  cd ${cwd}
fi

print_debug "Check all media"
for i in 1 2 3 4 5 ; do
  size=`du -b ${tmp}/TestVolume00$i|cut -f1`
  if test "$size" -lt 5000 ; then
     print_debug "ERROR 2: Volume TestVolum00$i is too small $size"
     ls -l ${tmp}/TestVolume00$i
     estat=2
  fi
done

cat <<EOF  > $tmp/bconcmds
@$out $tmp/log.err
truncate volume=TestVolume001 storage=vDrive-1 allpools
@$out $tmp/log3.out
truncate volume=TestVolume001 storage=vDrive-2 allpools
EOF

run_bconsole

if test "$debug" -eq 1 ; then
  echo "Volume TestVolume001 should be truncated ..."
  cd ${tmp}
  ls -l TestVolume*
  cd ${cwd}
fi

for i in  2 3 4 5 ; do
  size=`du -b ${tmp}/TestVolume00$i|cut -f1`
  if test "$size" -lt 5000 ; then
     print_debug "ERROR 3: Volume TestVolum00$i is too small $size"
     ls -l ${tmp}/TestVolume00$i
     estat=3
  fi
done
for i in 1 ; do
  size=`du -b ${tmp}/TestVolume00$i|cut -f1`
  if test "$size" -gt 5000 ; then
     print_debug "ERROR 4: Volume TestVolum00$i is not truncated (too big) $size"
     ls -l ${tmp}/TestVolume00$i
     estat=4
  fi
done

echo "truncate volume storage=vDrive-2 allpools" > $tmp/bconcmds
run_bconsole

if test "$debug" -eq 1 ; then
  echo "Volumes 001, 003, and 004 should be truncated ..."
  cd ${tmp}
  ls -l ${tmp}/TestVolume*
  cd ${cwd}
fi

for i in 2 5 ; do
  size=`du -b ${tmp}/TestVolume00$i|cut -f1`
  if test $size -lt 5000 ; then
     print_debug "ERROR 5: Volume TestVolum00$i is too small $size"
     ls -l ${tmp}/TestVolume00$i
     estat=5
  fi
done
for i in 1 3 4 ; do
  size=`du -b ${tmp}/TestVolume00$i|cut -f1`
  if test $size -gt 5000 ; then
     print_debug "ERROR 6: Volume TestVolum00$i is not truncated (too big) $size"
     ls -l ${tmp}/TestVolume00$i
     estat=6
  fi
done


cat <<END_OF_DATA > $tmp/bconcmds
@$out $tmp/log1.out
setdebug level=0 dir
setbandwidth limit="1000000 kb/s" client
wait
messages
@$out $tmp/log2.out
restore jobid=5 all done yes where=$tmp/bacula-restores
wait
messages
@#########################################################
@# Display catalog settings for Pool and Media
@#########################################################
@$out $tmp/log4.out
setdebug level=0 director
sql
select VolumeName, ActionOnPurge FROM Media;
select Name, ActionOnPurge FROM Pool;

wait
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File

stop_bacula

check_two_logs

check_restore_diff

print_debug "Test if Pool record is ok"
r=`awk '/Default/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ActionOnPurge on Pool record should be 1 ($r)"
    estat=2
fi

print_debug "Test TestVolume001 if Media record is ok"
r=`awk '/TestVolume001/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ActionOnPurge on Media record should be 1"
    estat=2
fi

print_debug "Test TestVolume002 if Media record is ok"
r=`awk '/TestVolume002/ { print $4 }' $tmp/log4.out`
if [ "$r" != 0 ]; then
    print_debug "ActionOnPurge on Media record should be 0"
    estat=2
fi

end_test
